import {otherRouter} from '@/router/router';
import appRouter from '@/router/appRouter';
import Util from '@/libs/util';

const app = {
    state: {
        cachePage: [],
        lang: '',
        isFullScreen: false,
        openedSubmenuArr: [], // 要展开的菜单数组
        menuTheme: 'dark', // 主题,
        menuList: [], // 菜单
        refresh: true, // 页面是否刷新
        pageOpenedList: [{
            title: '首页',
            path: '/home',
            name: 'home_index'
        }],
        currentPath: [
            {
                title: '首页',
                path: '',
                name: 'home_index'
            }
        ], // 面包屑数组
        routers: [
            otherRouter,
            ...appRouter
        ],
        tagsList:[
            otherRouter,
            ...appRouter
        ],
        currentPageName: '',
        dontCache: ['text-editor', 'artical-publish'] // 在这里定义你不想要缓存的页面的name属性值(参见路由配置router.js)
    },
    mutations: {

        /**
         *
         * @param state
         * @param name
         */
        setCurrentPageName (state, name) {
            state.currentPageName = name;
        },
        /**
         * 记录打开菜单
         * @param state
         * @param name
         */
        addOpenSubmenu (state, name) {
            let hasThisName = false;
            let isEmpty = false;
            if (name.length === 0) {
                isEmpty = true;
            }
            if (state.openedSubmenuArr.indexOf(name) > -1) {
                hasThisName = true;
            }
            if (!hasThisName && !isEmpty) {
                state.openedSubmenuArr.push(name);
            }

            localStorage.openedSubmenuArr = JSON.stringify(state.openedSubmenuArr);
        },
        /**
         * 设置面包屑
         * @param state
         * @param pathArr
         */
        setCurrentPath (state, pathArr) {
            state.currentPath = pathArr;
        },

        /**
         * 打开tab
         * @param state
         * @param get
         */
        pageOpenedList (state, get) {
            let openedPage = state.pageOpenedList[get.index];
            if (get.argu) {
                openedPage.argu = get.argu;
            }
            if (get.query) {
                openedPage.query = get.query;
            }
            state.pageOpenedList.splice(get.index, 1, openedPage);
            //写入本地
            localStorage.pageOpenedList = JSON.stringify(state.pageOpenedList);
        },

        /**
         * 关闭所有tag
         * @param state
         */
        clearAllTags (state) {
            state.pageOpenedList.splice(1);
            state.cachePage.length = 0;
            localStorage.pageOpenedList = JSON.stringify(state.pageOpenedList);
        },
        /**
         * 关闭其他tag
         * @param state
         * @param vm
         */
        clearOtherTags (state, vm) {
            let currentName = vm.$route.name;
            let currentIndex = 0;
            state.pageOpenedList.forEach((item, index) => {
                if (item.name === currentName) {
                    currentIndex = index;
                }
            });
            if (currentIndex === 0) {
                state.pageOpenedList.splice(1);
            } else {
                state.pageOpenedList.splice(currentIndex + 1);
                state.pageOpenedList.splice(1, currentIndex - 1);
            }
            let newCachepage = state.cachePage.filter(item => {
                return item === currentName;
            });
            state.cachePage = newCachepage;
            localStorage.pageOpenedList = JSON.stringify(state.pageOpenedList);
        },

        /**
         * 关闭tab
         * @param state
         * @param name
         */
        closePage (state, name) {
            state.cachePage.forEach((item, index) => {
                if (item === name) {
                    state.cachePage.splice(index, 1);
                }
            });
        },
        /**
         * 移除tag
         * @param state
         * @param name
         */
        removeTag (state, name) {
            state.pageOpenedList.map((item, index) => {
                if (item.name === name) {
                    state.pageOpenedList.splice(index, 1);
                }
            });
        },
        setOpenedList (state) {
            state.pageOpenedList = localStorage.pageOpenedList ? JSON.parse(localStorage.pageOpenedList) : [otherRouter.children[0]];
            state.openedSubmenuArr = localStorage.openedSubmenuArr ? JSON.parse(localStorage.openedSubmenuArr) : [];
        },
        setAvator (state, path) {
            localStorage.avatorImgPath = path;
        },
        clearOpenedSubmenu (state) {
            state.openedSubmenuArr.length = 0;
            //清除tab-page,清除面包屑
            localStorage.clear();

            state.currentPath = [{
                title: '首页',
                path: '',
                name: 'home_index'
            }];
        },
        increateTag (state, tagObj) {
            if (!Util.oneOf(tagObj.name, state.dontCache)) {
                state.cachePage.push(tagObj.name);
                localStorage.cachePage = JSON.stringify(state.cachePage);
            }
            state.pageOpenedList.push(tagObj);
            localStorage.pageOpenedList = JSON.stringify(state.pageOpenedList);
        }
    }
}

export default app;